<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-lx-calendar"></i> 配置管理</el-breadcrumb-item>
                <el-breadcrumb-item>配置列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button @click="delAll" class="handle-del mr10" icon="el-icon-delete" type="primary">批量删除</el-button>
                <el-input class="handle-input mr10" placeholder="输入筛选关键词" v-model="select_word"></el-input>
            </div>
            <el-table :data="data" @selection-change="handleSelectionChange" border class="table" ref="multipleTable"
                      stripe>
                <el-table-column align="center" type="selection" width="55"></el-table-column>

                <el-table-column align="center" label="序号" type="index" width="55"></el-table-column>

                <el-table-column label="配置名称" prop="name" width="250">
                </el-table-column>

                <el-table-column label="所属项目" prop="interface.project" width="250">
                </el-table-column>

                <el-table-column label="所属接口" prop="interface.name" width="250">
                </el-table-column>

                <el-table-column align="center" label="编辑人员" prop="author" width="100">
                </el-table-column>

                <el-table-column align="center" label="操作">
                    <template slot-scope="scope">
                        <el-button @click="linkTo(scope.row.id)" icon="el-icon-edit" type="text">编辑</el-button>
                        <el-button @click="handleDelete(scope.$index, scope.row)" class="red" icon="el-icon-delete"
                                   type="text">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination :page-size="page_size" :page-sizes="[4, 5, 8, 10, 20]"
                               :total="total_nums" @current-change="handleCurrentChange"
                               @size-change="handleSizeChange" background
                               layout="total, sizes, prev, pager, next, jumper">
                </el-pagination>
            </div>
        </div>

        <!-- 删除提示框 -->
        <el-dialog :visible.sync="delVisible" center title="删除配置" width="300px">
            <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
            <span class="dialog-footer" slot="footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button @click="deleteRow" type="primary">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
    import { configures_list, delete_configure } from '../../api/api';

    export default {
        name: 'basetable',
        data() {
            return {
                tableData: [],
                cur_page: 1,    // 当前页
                page_size: 10,  // 每页显示的数量
                total_nums: 1, // 数据总条数

                multipleSelection: [],
                select_word: '',
                del_list: [],
                editVisible: false,
                delVisible: false,
                runVisible: false,
                form: {},

                idx: -1,   // 在tableData数组中的索引值
                id: -1    // 在数据库中的真实索引值
            };
        },
        created() {
            this.getData();
        },
        computed: {
            data() {
                return this.tableData.filter((d) => {
                    let is_del = false;
                    for (let i = 0; i < this.del_list.length; i++) {
                        if (d.name === this.del_list[i].name) {
                            is_del = true;
                            break;
                        }
                    }
                    if (!is_del) {
                        if (d.name.indexOf(this.select_word) > -1 ||
                            d.interface.project.indexOf(this.select_word) > -1 ||
                            d.interface.name.indexOf(this.select_word) > -1 ||
                            d.author.indexOf(this.select_word) > -1
                        ) {
                            return d;
                        }
                    }
                });
            }
        },
        methods: {
            // 分页导航
            handleCurrentChange(val) {
                this.cur_page = val;
                this.getData();
            },
            handleSizeChange(val) {
                // console.log('val', val);
                this.page_size = val;
                this.getData();
            },
            getData() {
                configures_list({
                    'page': this.cur_page,
                    'size': this.page_size
                })
                    .then(response => {
                        this.tableData = response.data.results;
                        this.cur_page = response.data.current_page_num || 1;
                        this.total_nums = response.data.count || 1;
                    });
            },
            search() {
                this.is_search = true;
            },
            handleDelete(index, row) {
                this.idx = index;
                this.id = row.id;
                this.delVisible = true;
            },
            delAll() {
                const length = this.multipleSelection.length;
                let str = '';
                this.del_list = this.del_list.concat(this.multipleSelection);
                for (let i = 0; i < length; i++) {
                    str += this.multipleSelection[i].name + ' ';
                    delete_testcase(this.multipleSelection[i].id)
                        .then(response => {

                        })
                        .catch(error => {
                            this.$message.error('服务器错误');
                        });
                }

                this.$message.error('删除了' + str);
                this.multipleSelection = [];
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 确定删除
            deleteRow() {
                delete_configure(this.id)
                    .then(response => {
                        this.$message.success('删除成功');
                        this.delVisible = false;
                        if (this.tableData[this.idx].id === this.id) {
                            this.tableData.splice(this.idx, 1);
                        } else {
                            for (let i = 0; i < this.tableData.length; i++) {
                                if (this.tableData[i].id === this.id) {
                                    this.tableData.splice(i, 1);
                                    return;
                                }
                            }
                        }
                    })
                    .catch(error => {
                        this.$message.error('服务器错误');
                    });
            },
            linkTo(id) {
                this.$router.push({ path: `/configures_edit/${id}` });
            }
        }
    };

</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }

    .del-dialog-cnt {
        font-size: 16px;
        text-align: center
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }

    .mr10 {
        margin-right: 10px;
    }
</style>
